var gridList
    ,layer
    ,moduleId
    ,selectedModule = {};
$(document).ready(function () {
    layui.use(['layer'], function () {
        layer = layui.layer;
    });
    gridList = new dataGrid("gridList");
    loadTree();
});
/*
* @author: wj
* @description: 加载模块
* @date: 2019/3/2 15:56
* @param   
* @return   
*/
function loadTree() {
    ActHttpRequest('/wf/module/list', {"moduleState": "1"}, function(data){
        if (data.OK === "OK") {
            if (data.List.length > 0) {
                selectedModule.moduleId = data.List[0].moduleId;
                selectedModule.moduleName = data.List[0].moduleName;
                gridList.load({
                    'moduleId': selectedModule.moduleId,
                    'deleteFlag': 1
                });

                $("#title span").html(data.List[0].moduleName);
                var str = "";
                data.List.forEach(function (value) {
                    str += "<li><a onclick=\"selectModule('" + value.moduleId + "','" + value.moduleName + "')\">" +
                        "<i class='layui-icon' style='top:1px;position:relative;' id='" + value.moduleId + "'></i>" +
                        "<cite>" + value.moduleName + "</cite>" +
                        "</a></li>";
                });
                $("#Tree").html(str);
                $("#Tree li i").html("&#xe620;");
                $("#Tree li i").eq(0).html("&#xe614;");
            }
        } else {
            layer.msg('取得模块失败!', {
                icon: 1,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {
            })
        }
    }, 'post', null, null, null, null);
}
/*
* @author: wj
* @description: 模块选择
* @date: 2019/3/2 16:30  
* @param   
* @return   
*/
function selectModule(tree_moduleId, moduleName) {
    $("#Tree i").html("&#xe620;");
    $("#Tree #" + tree_moduleId).html("&#xe614;");
    $("#title span").html(moduleName);
    selectedModule.moduleId = tree_moduleId;
    selectedModule.moduleName = moduleName;
    moduleId = tree_moduleId;
    load_wfWorkflow();
}
function load_wfWorkflow(){
    gridList.reload({
        'deleteFlag': 1
        ,'moduleId': moduleId
        , 'wfName': ''
    });
}
/*
* @author: wj
* @description: 流程状态格式化
* @date: 2019/3/2 16:33
* @param
* @return
*/
function activeFlagRenderer1(e) {
    var e = e || window.event,
        row = e.record,
        state = row.activeFlag;
    if (state === 1){
        return '启用';
    } else if (state === 2) {
        return '关闭中';
    }else{
        return '已关闭';
    }
}
/*
* @author: wj
* @description: 刷新
* @date: 2019/3/9 10:03
* @param
* @return
*/
function refresh_data(){
    $("#key").val("");
    gridList.reload({
        'deleteFlag': 1
        , 'moduleId': moduleId
    });

}
/*
* 打开流程回收站*/
function doTrash() {
    layer.open({
        type: 2,
        title: "流程回收站",
        area: ['1000px', '600px'],
        fixed: false, //不固定
        maxmin: true,
        content: 'wfWorkFlowRecycle.html?moduleId=' + selectedModule.moduleId || '' + "&temp=" + new Date().getUTCMilliseconds(),
        end: function () {
            gridList.reload();
        }
    });
}
//查询
function search_data() {
    var key = $('#key').val()
        ,param = {
        'deleteFlag': 1
        , 'moduleId': moduleId
        , 'wfName': key
    };
    gridList.reload(param);
    $("#key").val("");

}

function formatOper(e) {
    var e = e || window.event,
        row = e.record,
        id = row.wfId,
        name = row.wfName,
        activeFlag = row.activeFlag,
        btn = "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' hideFocus='' href='javascript:;' lay-event='del' onClick='moveUp(\"" + id + "\")' style='margin-left: 4px; width: 70px'><i class='layui-icon' >&#xe619;</i>上移</a>" +
        "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' hideFocus='' href='javascript:;' lay-event='del' onClick='moveDown(\"" + id + "\")' style='margin-left: 4px;width: 70px'><i class='layui-icon'>&#xe61a;</i>下移</a>" +
        "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:;' onClick='doEdit(\"" + id + "\",\""+ activeFlag+"\")' style='width: 70px'><i class='layui-icon'>&#xe642;</i>修改</a>" +
        "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:;' onClick='doDesign(\"" + id + "\",\"" + name + "\",\"" + activeFlag + "\")' style='width: 70px'><i class='layui-icon'>&#xe632;</i>设计</a>" +
        "<a class='layui-btn layui-btn-danger layui-btn-xs' hideFocus='' href='javascript:;' onClick='removeData(\"" + id + "\",\"" + name + "\",\""+activeFlag+"\")' style='width: 70px'><i class='layui-icon'>&#xe640;</i>删除</a>";
    return btn;
}
/*
* @author: wj
* @description: 修改流程
* @date: 2019/3/9 10:40
* @param
* @return
*/
function doEdit(id,activeFlag) {
    var url = "wfWorkflowDetail.html?temp=" + new Date().getUTCMilliseconds();
    var title = '添加操作';
    if (id) {
        title = '修改操作';
        url += "&fid=" + id;
    }
    if (selectedModule.moduleName != null)
        url += '&moduleId=' + selectedModule.moduleId + '&moduleName=' + encodeURIComponent(selectedModule.moduleName) + '&activeFlag='+activeFlag;
    layer.open({
        type: 2,
        title: title,
        area: ['720px', '400px'],
        fixed: false, //不固定
        maxmin: true,
        content: url,
        end: function () {
            gridList.reload();
        }
    });
}
/*
* @author: wj
* @description: 删除流程(假删除)
* @date: 2019/3/9 10:41
* @param
* @return
*/
function removeData(fid, title, activeFlag) {
    if(activeFlag != 3){
        layer.msg("流程状态未关闭,不能删除!", {icon: 1, time: 1000});
        return;
    }
    parent.layer.confirm('确定要删除' + title + '么？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        ActHttpRequest("/wf/workflow/update", {"wfId": fid, 'deleteFlag': 2}, function (data) {
            if (data.OK == "OK") {
                parent.layer.msg('操作成功！', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    gridList.reload();
                })
            } else {
                parent.layer.msg('操作失败！', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}

/*
* @author: wj
* @description: 设计流程
* @date: 2019/3/9 10:40
* @param
* @return
*/
function doDesign(id, title, activeFlag) {
    // var url = "/wf/workflow/toDesigner";
    var url = "workflowDesigner.html";
    url += "?wfId=" + id + "&moduleId=" + selectedModule.moduleId + "&displayName=" + selectedModule.moduleName;
    title = "【" + title + "】" + "流程设计"
    layer.open({
        type: 2,
        title: title,
        content: url,
        area: ['100%', '100%'],
        success: function (layerObj) {

        }
    });
}
/*
* @author: wj
* @description: 复制流程
* @date: 2019/3/11 15:28
* @param
* @return
*/
function copyData(wfId){
    layer.confirm('确定复制?', {
        btn: ['确定', '取消'] //按钮
    }, function () {

        ActHttpRequest("wfWorkflow/copyorkFlow.do", {"id": wfId}, function (data) {
            if (data.OK === "OK") {
                layer.msg(data.msg, {icon: 1, time: 1000, shade: 0.3});
            } else {
                layer.msg("操作出错", {icon: 1, time: 1000, shade: 0.3});
            }
            gridList.reload();
        });

    }, function () {
    });
}
/*
* @author: wj
* @description: 双击查看详情
* @date: 2019/3/9 10:41
* @param
* @return
*/
function details(e) {
    var url = "./wfWorkflowDetail.html?2=2&fid=" + e.wfId;
    if (selectedModule.moduleName != null)
        url += '&moduleId=' + selectedModule.moduleId + '&moduleName=' + selectedModule.moduleName;

    layer.open({
        type: 2,
        area: ['720px', '560px'],
        fixed: false, //不固定
        maxmin: true,
        content: url,
        end: function () {
            gridList.reload();
        }
    });
}

//上移
function moveUp(e) {
    console.log(e)
    ActHttpRequest("/wf/workflow/moveUp?id=" + e , {}, function (data) {
        if (data.OK == "OK") {
            layer.msg(data.msg, {icon: 1, time: 1000, shade: 0.3});
        } else {
            layer.msg("操作出错", {icon: 1, time: 1000, shade: 0.3});
        }
        gridList.reload();
    }, 'post', null, null, null, null);
}

//下移
function moveDown(e) {
    ActHttpRequest("/wf/workflow/moveDown?id=" + e, {}, function (data) {
        if (data.OK == "OK") {
            layer.msg(data.msg, {icon: 1, time: 1000, shade: 0.3});
        } else {
            layer.msg("操作出错", {icon: 1, time: 1000, shade: 0.3});
        }
        gridList.reload();
    });
}

